// main.js
import uviewPlus, { setConfig } from 'uview-plus'
import App from './App'
import { setupPinia } from './store/index';

Number.prototype.wtoFixed = function (d) {
  let s = `${this}`;
  /* eslint-disable-next-line */
  if (!d) d = 0;
  if (s.indexOf(".") === -1) s += ".";
  s += new Array(d + 1).join("0");
  if (new RegExp(`^(-|\\+)?(\\d+(\\.\\d{0,${d + 1}})?)\\d*$`).test(s)) {
    s = `0${RegExp.$2}`;
    const pm = RegExp.$1;
    let a = RegExp.$3.length;
    let b = true;
    if (a === d + 2) {
      a = s.match(/\d/g);
      if (parseInt(a[a.length - 1], 10) > 4) {
        for (let i = a.length - 2; i >= 0; i--) {
          a[i] = parseInt(a[i], 10) + 1;
          if (a[i] === 10) {
            a[i] = 0;
            b = i !== 1;
          } else break;
        }
      }
      s = a.join("").replace(new RegExp(`(\\d+)(\\d{${d}})\\d$`), "$1.$2");
    }
    if (b) s = s.substr(1);
    return (pm + s).replace(/\.$/, "");
  }
  return `${this}`;
};

// #ifdef APP-PLUS
plus.globalEvent.addEventListener("receiveData", function(e) {
  console.log("副屏发给主屏的消息", e);
  // 广播出去副屏发来的消息
  uni.$emit('receiveDataChange', e)
});
// #endif

// #ifndef VUE3
import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
  ...App
})
setupPinia(app);
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp () {
  const app = createSSRApp(App)
  setupPinia(app);
  app.use(uviewPlus)

  return {
    app
  }
}
// #endif
